package com.weavernorth.action.humanresource;

import com.weavernorth.util.Logger4jUtil;
import com.weavernorth.util.MyCommonUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Iterator;

/**
 * @author： 王文彬
 * @date： 2020/10/22 18:07
 * @description： 调薪日期在SAP校验
 */
public class CheckTxDate implements Action {
    private Logger log = Logger4jUtil.getLoggerByName("/action/humanresource/CheckTxDate/");

    @Override
    public String execute(RequestInfo requestInfo) {
        log.info("调薪日期接口调用开始：");
        BaseBean bb=new BaseBean();
        RecordSet rs= new RecordSet();
        RecordSet rs_new= new RecordSet();
        //请求id
        String strRequestid = requestInfo.getRequestid();
        log.info("流程requestid=" + strRequestid);
        String strTable = requestInfo.getRequestManager().getBillTableName();
        log.info("表名：" + strTable);
        String strSql="select a.* from "+strTable+" a where a.requestid="+strRequestid+"";
        log.info("执行查询语句："+strSql);
        rs.executeQuery(strSql);
        String strygh="";
        String strDate="";
        //报文请求体
        StringBuffer requestXml = new StringBuffer();
        //拼接请求体
        requestXml.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">" +
                "<soapenv:Header/><soapenv:Body><urn:ZHRI_087>");
        while (rs.next()){
            strygh = Util.null2String(rs.getString("ygh"));
            log.info("员工号：" + strygh);
            strDate = Util.null2String(rs.getString("sxrq"));
            log.info("调薪生效日期：" + strDate);
            requestXml.append("<IV_BEGDA><![CDATA["+strDate+"]]></IV_BEGDA>");
            requestXml.append("<IV_PERNR><![CDATA["+strygh+"]]></IV_PERNR>");
        }
        requestXml.append("<RETURN>");
        requestXml.append("<item>");
        requestXml.append("<TYPE></TYPE>");
        requestXml.append("<ID></ID>");
        requestXml.append("<NUMBER></NUMBER>");
        requestXml.append("<MESSAGE></MESSAGE>");
        requestXml.append("<LOG_NO></LOG_NO>");
        requestXml.append("<LOG_MSG_NO></LOG_MSG_NO>");
        requestXml.append("<MESSAGE_V1></MESSAGE_V1>");
        requestXml.append("<MESSAGE_V2></MESSAGE_V2>");
        requestXml.append("<MESSAGE_V3></MESSAGE_V3>");
        requestXml.append("<MESSAGE_V4></MESSAGE_V4>");
        requestXml.append("<PARAMETER></PARAMETER>");
        requestXml.append("<ROW></ROW>");
        requestXml.append("<FIELD></FIELD>");
        requestXml.append("<SYSTEM></SYSTEM>");
        requestXml.append("</item>");
        requestXml.append("</RETURN></urn:ZHRI_087> </soapenv:Body></soapenv:Envelope>");
        log.info("推送的xml:" + requestXml.toString());
        String strSendUrl = bb.getPropValue("xcddCheck", "xcddUrl");
        log.info("薪酬调动校验有效日期接口地址："+strSendUrl);
        try {
            log.info("执行发送请求：");
            //获取用户名
            String strUsername = Util.null2String(bb.getPropValue("personnel-erdos", "username"));
            //获取密码
            String strPassword = Util.null2String(bb.getPropValue("personnel-erdos", "password"));
            String responseXml = MyCommonUtils.sendPost(strSendUrl, requestXml.toString(), "utf-8",strUsername,strPassword);
            log.info("返回的xml:" + responseXml);
            //返回标识
            String strReturnStatus = "";
            //返回信息
            String strMessage = "";
            String[] strings = MyCommonUtils.responseResult(responseXml);
            log.info("xml解析结果："+strings.toString());
            strReturnStatus=strings[0];
            strMessage=strings[1];
             if ("".equals(strReturnStatus)||!"S".equals(strReturnStatus)) {
                requestInfo.getRequestManager().setMessagecontent("接口调用失败,错误信息："+strMessage);
                return FAILURE_AND_CONTINUE;
            }

        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("程序异常："+e.toString());
            return FAILURE_AND_CONTINUE;
        }
        log.info("调薪日期接口调用结束。。。");
        return SUCCESS;
    }
}
